package com.ruoyi.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaIgnore;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.domain.SysConfig2;
import com.ruoyi.mapper.SysConfig2Mapper;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@SaCheckLogin
@Api(tags="配置参数")
@RequestMapping("/ray/config")
public class ApiConfigRayContorller extends BaseController {

    @Autowired
    private SysConfig2Mapper sysConfig2Mapper;


    @GetMapping("/list")
    @ApiOperation("获取配置列表")
    public TableDataInfo list(@RequestParam(name = "configKey",required = false,defaultValue = "") String configKey)
    {
        QueryWrapper<SysConfig2> wp = new QueryWrapper();

        if(!configKey.isEmpty()){
            System.out.println("configKey = " + configKey);
            wp.eq("config_key",configKey);
        }else {
            wp.like("config_key","ray.");
        }

        List<SysConfig2> list = sysConfig2Mapper.selectList(wp);
        return getDataTable(list);
    }


    @SaIgnore
    @GetMapping("/get_key")
    @ApiOperation("获取某个配置")
    public AjaxResult get(@RequestParam(name = "configKey",required = true,defaultValue = "") String configKey)
    {
        if(!configKey.isEmpty()){
            QueryWrapper<SysConfig2> wp = new QueryWrapper();
            wp.eq("config_key",configKey);
            SysConfig2 list = sysConfig2Mapper.selectOne(wp);
            return AjaxResult.success(configKey,list);
        }else {
          return AjaxResult.error("请传递key");
        }
    }


    @PostMapping("/update_key")
    @ApiOperation("更新某个配置")
    public AjaxResult update(SysConfig2 sysConfig2)
    {
        QueryWrapper<SysConfig2> wp = new QueryWrapper();
        Integer res = sysConfig2Mapper.updateById(sysConfig2);
        if(res>0){
            return AjaxResult.success("更新成功",sysConfig2);
        }else {
            return AjaxResult.error("未更新",sysConfig2);
        }


    }




}
